#! /bin/bash
# dwm状态栏刷新脚本
# > 注: 本脚本图标字体为JetBrainsMono Nerd Font下的字符，若没安装可能一些图标字符会显示不正确。
# 
# [嵌入式Linux下获取CPU温度方法_Acuity.的博客-CSDN博客_linux 查看cpu温度](https://blog.csdn.net/qq_20553613/article/details/107703442#)

# source ~/.profile

# 打印输出其他状态 如：蓝牙，下载等
print_others() {
    icons=()
    # [ "$(docker ps | grep v2raya)" ] && icons=(${icons[@]} "")
    # [ "$(docker ps | grep 'arch')" ] && icons=(${icons[@]} "")
    # [ "$(bluetoothctl info 64:03:7F:7C:81:15 | grep 'Connected: yes')" ] && icons=(${icons[@]} "")
    # [ "$(bluetoothctl info 8C:DE:F9:E6:E5:6B | grep 'Connected: yes')" ] && icons=(${icons[@]} "")
    # [ "$(bluetoothctl info 34:02:86:E1:57:A2 | grep 'Connected: yes')" ] && icons=(${icons[@]} "")
    # [ "$(ps -aux | grep 'danmu_sender' | sed 1d)" ] && icons=(${icons[@]} "ﳲ")
    # [ "$(ps -aux | grep 'aria2c' | sed 1d)" ] && icons=(${icons[@]} "")
    [ "$AUTOSCREEN" = "OFF" ]  && icons=(${icons[@]} "ﴸ")

    if [ "$icons" ]; then
        text=" ${icons[@]} "
        printf "%s" "$text" 
    fi
}

# Docker
print_docker(){
    # 图标 
    docker_icon=""
    printf "%s" "$docker_icon" 
}

# 蓝牙
print_bluetooth() {
    # 图标         
    # 耳机图标 
    # 获取本机蓝牙设备地址

    if command -v bluetoothctl &> /dev/null; then # 检查bluetoothctl是否安装
        # 检查蓝牙服务状态
        if systemctl is-active --quiet bluetooth; then
            echo "蓝牙服务正在运行"
            bluetoothctl_address=$(bluetoothctl list | awk '{printf $2}') 
            # 获取本机蓝牙设备连接状态
            # bluetoothctl info $(bluetoothctl list | awk '{print $2}' | grep 'Connected: yes' )  
            bluetoothctl_status=$(bluetoothctl info $bluetoothctl_address | grep 'Connected: yes') 
            # 获取本机蓝牙待连接设备数量（附近蓝牙设备数量）
            bluetoothctl_num=$(bluetoothctl devices | wc -l) 
            if [ $(bluetoothctl show | grep "Powered: yes" | wc -c) -eq 0 ]; then
                    # echo ""
                    bluetooth_icon=" "
            else
                # if [ "$(bluetoothctl info $bluetoothctl_address | grep 'Connected: yes')" ]; then
                #     # bluetooth_icon=" "
                # elif [ "$(bluetoothctl info $bluetoothctl_address | grep 'not available')" ]; then
                #     bluetooth_icon=" "
                # else
                #     # 没有蓝牙或设备驱动有问题，或   ﰸ ﳠ ﮤ ﯇ ﮖ      
                #     # bluetooth_icon=" "
                #     bluetooth_icon=" "
                # fi
                # on
                if [ $(echo info | bluetoothctl | grep 'Device' | wc -c) -eq 0 ]; then
                        # echo ""
                        bluetooth_icon=" "
                else
                        # echo ""
                        bluetooth_icon=" "
                fi
            fi
        else
            # echo "警告：蓝牙服务未运行！"
            # echo "请执行以下命令启动服务："
            # echo "sudo systemctl start bluetooth"
            # echo ""
            # echo "启用开机自动启动："
            # echo "sudo systemctl enable bluetooth"
            bluetooth_icon=" ﰸ"
        fi

    else
        # 没有安装包含bluetoothctl命令的软件
        bluetooth_icon=" ﰸ"
    fi

    printf "%s" "$bluetooth_icon" 
}

# 打印 CPU 占用情况
print_cpu() {
    cpu_icon="閭"
    cpu_text=$(top -n 1 -b | sed -n '3p' | awk '{printf "%02d%", 100 - $8}')

    text=" $cpu_icon $cpu_text"
    printf "%s" "$text" 
}

# 打印 CPU 温度
print_cpu_temperature(){
    # echo  $[$(cat /sys/class/thermal/thermal_zone0/temp)/1000]°C
    # echo " 🌡︎"$[$(cat /sys/class/thermal/thermal_zone0/temp)/1000]"°C"
    # cpu原厂提供的linux内核通常已经带有cpu温度检测驱动，并将温度信息映射到用户文件系统下，用户只需从该虚拟文件读取温度信息即可。
    # cpu温度虚拟文件位于“/sys/devices/virtual/thermal”或者“/sys/class/thermal”下，命名为“thermal_zoneX”，X表示cpu核温度检测节点，如果是多核cpu通常会有多个温度检测节点；使用其中一个即可，因为温度通常差异不大。
    # cpu0：
    # cat /sys/class/thermal/thermal_zone0/temp
    # cpu1：
    # cat /sys/class/thermal/thermal_zone1/temp
    # 其中温度单位是0.001℃，即是温度节点0“thermal_zone0”当前温度为42.222℃。
    # 图标：🌡︎ 﨎 﨏         
    cpu_temperatur_icon="﨏"  
    # cpu_temperatur_text=$(top -n 1 -b | sed -n '3p' | awk '{printf "%02d%", 100 - $8}')
    cpu_temperatur_value=$[$(cat /sys/class/thermal/thermal_zone0/temp)/1000]
    # -eq 等于 -lt 小于 -ne 不等于 -gt 大于 -le 小于等于 -ge 大于等于
    if   [ "$cpu_temperatur_value" -le 20 ]; then cpu_temperatur_icon="";
    elif [ "$cpu_temperatur_value" -le 40 ]; then cpu_temperatur_icon="";
    elif [ "$cpu_temperatur_value" -le 60 ]; then cpu_temperatur_icon="";
    elif [ "$cpu_temperatur_value" -le 75 ]; then cpu_temperatur_icon="";
    elif [ "$cpu_temperatur_value" -le 85 ]; then cpu_temperatur_icon="";
    elif [ "$cpu_temperatur_value" -gt 86 ]; then cpu_temperatur_icon="";
    else cpu_temperatur_icon="﨏" ; 
    fi
    text=" $cpu_temperatur_icon $cpu_temperatur_value"
    printf "%s°C" "$text"
}


# 打印内存使用情况
print_mem() {
    mem_total=$(cat /proc/meminfo | grep "MemTotal:"|awk '{print $2}')
    mem_free=$(cat /proc/meminfo | grep "MemFree:"|awk '{print $2}')
    mem_buffers=$(cat /proc/meminfo | grep "Buffers:"|awk '{print $2}')
    mem_cached=$(cat /proc/meminfo | grep -w "Cached:"|awk '{print $2}')
    men_usage_rate=$(((mem_total - mem_free - mem_buffers - mem_cached) * 100 / mem_total))
	mem_icon=""
    mem_text=$(echo $men_usage_rate | awk '{printf "%02d%", $1}')
    text=" $mem_icon $mem_text"
    printf "%s" "$text" 
}

# 打印 硬盘容量 使用情况
print_disk() {
    # df -h --output=target,used,size,pcent
    # df -h /dev/sda1 | awk ' /[0-9]/ {print $3 "/" $2}'
    # df -h / | awk ' /[0-9]/ {print $3 "/" $2}'
    # disk_text="$(df -h / | awk ' /[0-9]/ {print $3 "/" $2}')"
    disk_text="$(df -h / | awk ' /[0-9]/ {print $5 }')"
    #   禍 
    disk_icon=""

    text=" $disk_icon $disk_text"
    printf "%s" "$text"
}

# 打印日期
print_date() {
    date_text="$(date '+%Y/%m/%d')"
    # date_text="$(date '+%Y-%m-%d')"
    #  
    date_icon=""

    text=" $date_icon $date_text"
    # text=" $date_text"
    printf "%s" "$text"
}

# 打印 时间
print_time() {
    time_text="$(date '+%H:%M')"
    case "$(date '+%I')" in
        "01") time_icon="" ;;
        "02") time_icon="" ;;
        "03") time_icon="" ;;
        "04") time_icon="" ;;
        "05") time_icon="" ;;
        "06") time_icon="" ;;
        "07") time_icon="" ;;
        "08") time_icon="" ;;
        "09") time_icon="" ;;
        "10") time_icon="" ;;
        "11") time_icon="" ;;
        "12") time_icon="" ;;
    esac

    text=" $time_icon $time_text"
    printf "%s" "$text"
}

# 音量
dwm_alsa() {
    #           
    vol_value=$(amixer get Master | tail -n1 | sed -r "s/.*\[(.*)%\].*/\1/");
    # -eq 等于 -lt 小于 -ne 不等于 -gt 大于 -le 小于等于 -ge 大于等于
    if   [ "$vol_value" -le 10  ]; then vol_icon="";
    elif [ "$vol_value" -le 25 ]; then vol_icon="奄";
    elif [ "$vol_value" -le 50 ]; then vol_icon="奔";
    elif [ "$vol_value" -le 75 ]; then vol_icon="奔";
    elif [ "$vol_value" -le 99 ]; then vol_icon="墳";
    # else vol_icon="墳"; 
    else vol_icon=""; 
    fi

    vol_text=$vol_value%
    text=" $vol_icon $vol_text"
    printf "%s" "$text" 
}

print_brightness() {
    # 图标：                               
    #    ﯷ ﱫ ﱪ ﳺ  盛 ﱳ ﲏ ﮻  盛 
    #         
    # 显卡设备 radeon_bl0  intel_backlight
    now_brightness_value=$(cat /sys/class/backlight/*/brightness);
    max_brightness_value=$(cat /sys/class/backlight/*/max_brightness);
    brightness_rate=$((now_brightness_value * 100 / max_brightness_value))
    brightness_percent=$(echo $brightness_rate | awk '{printf "%02d", $1}')
    # -eq 等于 -lt 小于 -ne 不等于 -gt 大于 -le 小于等于 -ge 大于等于
    # if   [ "$brightness_percent" -eq 0  ]; then brightness_icon="";
    if   [ "$brightness_percent" -eq 0  ]; then brightness_icon="";
    elif [ "$brightness_percent" -lt 10 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 20 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 30 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 40 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 50 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 60 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 70 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 80 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 80 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 90 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 99 ]; then brightness_icon="";
    elif [ "$brightness_percent" -eq 100 ]; then brightness_icon="盛";
    else brightness_icon=""; 
    fi
    brightness_text=$brightness_percent%
    text=" $brightness_icon $brightness_text"
    printf "%s" "$text" 
    # [Linux——屏幕亮度调节 - 走看看](http://t.zoukankan.com/droidxin-p-3695174.html)
    # [linux 命令获取屏幕亮度值，设置屏幕亮度_zyong_xu的博客-CSDN博客](https://blog.csdn.net/azure_2010/article/details/100978016)
    # [Linux下调节屏幕亮度(Intel核显)_Linux教程_Linux公社-Linux系统门户网站](https://www.linuxidc.com/Linux/2012-03/57098.htm)
    # [archlinux设置屏幕亮度_java_xiaoer的博客-CSDN博客](https://blog.csdn.net/java_xiaoer/article/details/51457620?locationNum=6&fps=1)
}

# 电池
print_bat() {
    # 图标      
    #  
    bat_text=$(acpi -b | sed 2d | awk '{print $4}' | grep -Eo "[0-9]+")
    [ ! "$bat_text" ] && bat_text=$(acpi -b | sed 2d | awk '{print $5}' | grep -Eo "[0-9]+")
    [ ! "$(acpi -b | grep 'Battery 0' | grep Discharging)" ] && charge_icon=""
    if  [ "$bat_text" -ge 95 ]; then charge_icon=""; bat_icon="";
    elif [ "$bat_text" -ge 90 ]; then bat_icon="";
    elif [ "$bat_text" -ge 80 ]; then bat_icon="";
    elif [ "$bat_text" -ge 70 ]; then bat_icon="";
    elif [ "$bat_text" -ge 60 ]; then bat_icon="";
    elif [ "$bat_text" -ge 50 ]; then bat_icon="";
    elif [ "$bat_text" -ge 40 ]; then bat_icon="";
    elif [ "$bat_text" -ge 30 ]; then bat_icon="";
    elif [ "$bat_text" -ge 20 ]; then bat_icon="";
    elif [ "$bat_text" -ge 10 ]; then bat_icon="";
    else bat_icon=""; fi

    bat_text=$bat_text%
    bat_icon=$charge_icon$bat_icon

    text=" $bat_icon $bat_text"
    color=$bat_color
    printf "%s" "$text" 
}

# 网络
print_wifi(){
    # 判断是否有w开头的设备（无线网卡 wifi设备）启动 
    case "$(cat /sys/class/net/w*/operstate 2>/dev/null)" in
        down) wifiicon="睊" ;;
        # up) wifiicon="$(awk '/^\s*w/ { print " " int($3 * 100 / 70) "%" }' /proc/net/wireless)" ;;
        up) wifiicon="$(awk '/^\s*w/ { print "" }' /proc/net/wireless)" ;;
    esac
    wifitext=" $wifiicon"
    printf "%s" "$wifitext"
}

print_eth(){
    # 判断是否有e开头的设备（有线网卡）启动
    #   ﴹ
    #      
    case "$(cat /sys/class/net/e*/operstate 2>/dev/null)" in
        down) ethicon="" ;;
        up) ethicon="" ;;
    esac
    ethtext=" $ethicon"
    printf "%s" "$ethtext"
}

# For example, with statuscmds defined as such:

# static const StatusCmd statuscmds[] = {
#     { "volume",  1 },
#     { "cpu",     2 },
#     { "battery", 3 },
# };
# And root name set like this:

# xsetroot -name "$(printf '\x01Volume |\x02 CPU |\x03 Battery')"
# Clicking on 'Volume |' would run volume, clicking on ' CPU |' would run cpu and clicking on ' Battery' would run battery.


# xsetroot -name "$(print_others)$(print_bluetooth)$(print_cpu)$(print_mem)$(print_disk)$(dwm_alsa)$(print_bat)$(print_time)$(print_date)"
# xsetroot -name "$(print_others)$(print_bluetooth)$(print_cpu)$(print_cpu_temperature)$(print_mem)$(print_disk)$(dwm_alsa)$(print_bat)$(print_time)$(print_date)$(print_wifi)$(print_eth)"
# xsetroot -name "$(print_others)$(print_bluetooth)$(print_cpu)$(print_cpu_temperature)$(print_mem)$(print_disk)$(dwm_alsa)$(print_bat)$(print_time)$(print_date)"

# xsetroot -name "$(printf '\x01Volume |\x02 CPU |\x03 Battery')"

# xsetroot -name "$(print_others)$(print_bluetooth)$(print_cpu)$(print_cpu_temperature)$(print_mem)$(print_disk)$(dwm_alsa)$(print_brightness)$(print_bat)$(print_time)"
# string_text="$(print_others)$(print_bluetooth)$(print_cpu)"
# res_text(){
#     ethtext=" $ethicon"
#     printf "%s" "$ethtext"
# }

# xsetroot -name "$(print_others)$(printf '\x06')$(print_bluetooth)$(printf '\x05')$(print_cpu)$(print_cpu_temperature)$(print_mem)$(printf '\x04')$(print_disk)$(printf '\x03')$(dwm_alsa)$(printf '\x07')$(print_brightness)$(printf '\x02')$(print_bat)$(printf '\x01')$(print_time)"
xsetroot -name "$(print_others)$(printf '\x06')$(printf '\x05')$(print_cpu)$(print_cpu_temperature)$(print_mem)$(printf '\x04')$(print_disk)$(printf '\x03')$(dwm_alsa)$(printf '\x07')$(print_brightness)$(printf '\x02')$(print_bat)$(printf '\x01')$(print_time)"

# echo "$(print_others)$(printf '\x06')$(print_bluetooth)$(printf '\x05')$(print_cpu)$(print_cpu_temperature)$(print_mem)$(printf '\x04')$(print_disk)$(printf '\x03')$(dwm_alsa)$(printf '\x07')$(print_brightness)$(printf '\x02')$(print_bat)$(printf '\x01')$(print_time)"


